package Hot100;

import java.util.*;

public class groupAnagrams {
    public static void main(String[] args) {
        String[] s = {"eat","tea","tan","ate","nat","bat"};
        List<List<String>> lists = groupAnagrams(s);
        Iterator<List<String >> outIterator = lists.iterator();
        while (outIterator.hasNext()){
            List<String> innernext = outIterator.next();
            Iterator<String> innerIterator = innernext.iterator();
            while (innerIterator.hasNext()){
                String value = innerIterator.next();
                System.out.println(value);
            }
        }
    }
    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for (String str : strs){
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
